/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.standard.webflux.metrics;

import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.Lists;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import lombok.Getter;
import org.springframework.util.CollectionUtils;
import org.thymeleaf.util.ArrayUtils;

import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.reflect.Array.newInstance;

/**
 * @project-name:standard-webflux
 * @package-name:com.hyts.standard.webflux.metrics
 * @author:LiBo/Alex
 * @create-date:2020-05-16 12:08
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public abstract class AbstractMetricsService implements MetricsService{

    /**
     * 复杂代理的注册中心
     */
    @Getter
    protected volatile CompositeMeterRegistry meterRegistries;

    /**
     * 安装相关注册中心服务
     * @param meterRegistries
     * @return
     */
    @Override
    public MetricsService install(MeterRegistry... meterRegistries) {
        // meterRegistry的操作对象
        if(Objects.isNull(this.meterRegistries)){
            synchronized (this) {
                if (Objects.isNull(this.meterRegistries)) {
                    this.meterRegistries = new CompositeMeterRegistry();
                }
            }
        }
        List<MeterRegistry> meterRegistryList = Lists.newArrayList(meterRegistries);
        //判断是否为空的集合
        if(CollectionUtils.isEmpty(meterRegistryList)){
            meterRegistryList.add(new SimpleMeterRegistry());
        }
        meterRegistryList.parallelStream().forEach(this.meterRegistries::add);
        return this;
    }

    /**
     * 标签
     * @param tag
     * @return
     */
    @Override
    public MetricsService tag(String... tag) {
        getMeterRegistries().config().commonTags(tag);
        return this;
    }

    /**
     * 标签
     * @param tag
     * @return
     */
    @Override
    public MetricsService tag(Map<String, String> tag) {
        Objects.requireNonNull(tag);
        String[] array = (String[]) newInstance(String.class,tag.size()*2);
        AtomicInteger integer = new AtomicInteger(0);
        tag.entrySet().parallelStream().forEach(param->{
            array[integer.getAndIncrement()] = param.getKey();
            array[integer.getAndIncrement()] = param.getValue();
        });
        getMeterRegistries().config().commonTags(array);
        return this;
    }

    @Override
    public MetricsService tag(List<Tag> tag) {
        getMeterRegistries().config().commonTags(tag);
        return this;
    }
}
